package com.haixiaoke.saas.enterprise.house.controller;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.constant.HttpStatus;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.PageUtils;
import com.haixiaoke.saas.house.domain.House;
import com.haixiaoke.saas.house.domain.HouseDynamic;
import com.haixiaoke.saas.house.service.impl.HouseDynamicServiceImpl;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 房态管理 Controller
 *
 * @author qixi
 * @date 2023-10-20
 */
@RestController
@RequestMapping("/enterprise/house/houseDynamic")
public class EnterpriseHouseDynamicController extends BaseController {

    @Autowired
    private HouseDynamicServiceImpl houseDynamicServiceImpl;


    /**
     * 查询房屋状态列表
     */
    @PreAuthorize("@ss.hasPermi('house:houseDynamic:houseStatusStatistics')")
    @GetMapping("/houseStatusStatistics")
    @TenantScope
    public AjaxResult houseStatusStatistics(House house) {
        house.setTenantId(TenantContext.getTenantId());
        HouseDynamic houseDynamic = houseDynamicServiceImpl.selectHouseStatusStatistics(house);
        return AjaxResult.success(houseDynamic);
    }


    /**
     * 查询房屋信息列表 卡片
     */
    @PreAuthorize("@ss.hasPermi('house:houseDynamic:findHouseInfoList')")
    @PostMapping("/findHouseInfoList")
    @TenantScope
    public AjaxResult list(@RequestBody House house) {
        house.setTenantId(TenantContext.getTenantId());
        Map houseDynamic = houseDynamicServiceImpl.selectHouseTreeList(house);
        return AjaxResult.success(houseDynamic);
    }

    /**
     * 查询房屋信息列表 列表
     */
    @PreAuthorize("@ss.hasPermi('house:houseDynamic:findHouseList')")
    @PostMapping("/findHouseList")
    @TenantScope
    public TableDataInfo findHouseList(@RequestBody House house) {
        house.setTenantId(TenantContext.getTenantId());
        List<House> houses = houseDynamicServiceImpl.selectHouseListByInfo(house);
        List<House> pageHouseList = PageUtils.setPage(houses);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(pageHouseList);
        rspData.setTotal(new PageInfo(houses).getTotal());
        return rspData;
    }

    /**
     * 房态查询-列表模式
     * @param house
     * @return
     */
    @PreAuthorize("@ss.hasPermi('house:houseDynamic:houseStatusStatistics')")
    @GetMapping("/conditionList")
    @TenantScope
    public TableDataInfo conditionList(House house){
        if (ObjectUtil.isEmpty(house.getStartTime()) || ObjectUtil.isEmpty(house.getEndTime())) {
            return getDataTable("请选择日期");
        }
        startPage();
        house.setTenantId(TenantContext.getTenantId());
        List<House> list = houseDynamicServiceImpl.conditionList(house);
        return getDataTable(list);
    }

    /**
     * 房态查询-卡片模式
     * @param house
     * @return
     */
    @PreAuthorize("@ss.hasPermi('house:houseDynamic:houseStatusStatistics')")
    @GetMapping("/conditionCard")
    @TenantScope
    public TableDataInfo conditionCard(House house){
        startPage();
        house.setTenantId(TenantContext.getTenantId());
        List<House> list = houseDynamicServiceImpl.conditionCard(house);
        return getDataTable(list);
    }
}
